#include "mainwindow.h"
#include "arraylistwindow.h"
#include "linkedlistwindow.h"
#include "arraystackwindow.h"
#include "binarytreewindow.h"
#include "completebinarytreewindow.h"
#include "bstwindow.h"
#include "huffmantreewindow.h"
#include "avltreewindow.h"
#include <QGridLayout>
#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QFont>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
}

MainWindow::~MainWindow()
{
}

void MainWindow::setupUI()
{
    setWindowTitle("数据结构可视化模拟器");
    setMinimumSize(600, 400);

    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    QLabel *titleLabel = new QLabel("数据结构可视化模拟器");
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(20);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);
    mainLayout->addWidget(titleLabel);

    QGridLayout *gridLayout = new QGridLayout();

    m_arrayListBtn = new QPushButton("顺序表");
    m_linkedListBtn = new QPushButton("链表");
    m_arrayStackBtn = new QPushButton("顺序栈");
    m_binaryTreeBtn = new QPushButton("二叉树");
    m_bstBtn = new QPushButton("二叉搜索树");
    m_huffmanTreeBtn = new QPushButton("哈夫曼树");
    m_avlTreeBtn = new QPushButton("AVL树");

    QString buttonStyle = "QPushButton { font-size: 14px; padding: 10px; }";
    m_arrayListBtn->setStyleSheet(buttonStyle);
    m_linkedListBtn->setStyleSheet(buttonStyle);
    m_arrayStackBtn->setStyleSheet(buttonStyle);
    m_binaryTreeBtn->setStyleSheet(buttonStyle);
    m_bstBtn->setStyleSheet(buttonStyle);
    m_huffmanTreeBtn->setStyleSheet(buttonStyle);
    m_avlTreeBtn->setStyleSheet(buttonStyle);

    gridLayout->addWidget(m_arrayListBtn, 0, 0);
    gridLayout->addWidget(m_linkedListBtn, 0, 1);
    gridLayout->addWidget(m_arrayStackBtn, 0, 2);
    gridLayout->addWidget(m_binaryTreeBtn, 1, 0);
    gridLayout->addWidget(m_bstBtn, 1, 1);
    gridLayout->addWidget(m_huffmanTreeBtn, 1, 2);
    gridLayout->addWidget(m_avlTreeBtn, 2, 0);

    mainLayout->addLayout(gridLayout);
    mainLayout->addStretch();

    connect(m_arrayListBtn, &QPushButton::clicked, this, &MainWindow::showArrayListWindow);
    connect(m_linkedListBtn, &QPushButton::clicked, this, &MainWindow::showLinkedListWindow);
    connect(m_arrayStackBtn, &QPushButton::clicked, this, &MainWindow::showArrayStackWindow);
    connect(m_binaryTreeBtn, &QPushButton::clicked, this, &MainWindow::showBinaryTreeSelection);
    connect(m_bstBtn, &QPushButton::clicked, this, &MainWindow::showBSTWindow);
    connect(m_huffmanTreeBtn, &QPushButton::clicked, this, &MainWindow::showHuffmanTreeWindow);
    connect(m_avlTreeBtn, &QPushButton::clicked, this, &MainWindow::showAVLTreeWindow);
}

void MainWindow::showBinaryTreeSelection()
{
    // 创建选择对话框
    QDialog *dialog = new QDialog(this);
    dialog->setWindowTitle("选择二叉树类型");
    dialog->setFixedSize(300, 150);

    QVBoxLayout *layout = new QVBoxLayout(dialog);

    // 标题
    QLabel *label = new QLabel("请选择二叉树存储结构:");
    label->setAlignment(Qt::AlignCenter);
    QFont font = label->font();
    font.setPointSize(12);
    label->setFont(font);
    layout->addWidget(label);

    // 按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();

    QPushButton *linkedBtn = new QPushButton("二叉链表");
    QPushButton *sequentialBtn = new QPushButton("顺序存储");

    linkedBtn->setMinimumSize(120, 40);
    sequentialBtn->setMinimumSize(120, 40);

    buttonLayout->addWidget(linkedBtn);
    buttonLayout->addWidget(sequentialBtn);

    layout->addLayout(buttonLayout);

    // 连接按钮信号
    connect(linkedBtn, &QPushButton::clicked, dialog, [this, dialog]() {
        dialog->accept();
        this->showLinkedBinaryTreeWindow();
    });

    connect(sequentialBtn, &QPushButton::clicked, dialog, [this, dialog]() {
        dialog->accept();
        this->showSequentialBinaryTreeWindow();
    });

    // 显示对话框
    dialog->exec();
    delete dialog;
}

void MainWindow::showLinkedBinaryTreeWindow()
{
    // 这里调用你原来的二叉树窗口
    BinaryTreeWindow *window = new BinaryTreeWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &BinaryTreeWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showSequentialBinaryTreeWindow()
{
    // 这里调用顺序存储二叉树窗口 - 修复类名
    CompleteBinaryTreeWindow *window = new CompleteBinaryTreeWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &CompleteBinaryTreeWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showArrayListWindow()
{
    ArrayListWindow *window = new ArrayListWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &ArrayListWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showLinkedListWindow()
{
    LinkedListWindow *window = new LinkedListWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &LinkedListWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showArrayStackWindow()
{
    ArrayStackWindow *window = new ArrayStackWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &ArrayStackWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showBSTWindow()
{
    BSTWindow *window = new BSTWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &BSTWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showHuffmanTreeWindow()
{
    HuffmanTreeWindow *window = new HuffmanTreeWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &HuffmanTreeWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::showAVLTreeWindow()
{
    AVLTreeWindow *window = new AVLTreeWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    connect(window, &AVLTreeWindow::returnToMainMenu, this, &MainWindow::onChildWindowClosed);
    window->show();
    this->hide();
}

void MainWindow::onChildWindowClosed()
{
    this->show();
}
